package controllers;

import java.util.LinkedList;
import java.util.List;

import javax.swing.text.html.parser.Entity;

import org.apache.commons.mail.EmailException;

import models.Eentity;
import models.Organization;
import models.Role;
import models.Topic;
import models.User;
import play.mvc.Controller;
import play.mvc.With;

//Created By C2 , Author IK_Ibrahim Kessba
@With(Secure.class)					//this class is used for the login, Doaa El-Hawary
public class Entities extends Controller {

	public static void index() {
		render();
	}
	
	/**
	 * calls show.html in Entity folder which renders based on the Entity id
	 * for instance a user would click on a link surrounded with <a href="@Enteties.show(entety.id)"></a>
	 * the method then check a couple of things before rendering
	 * 
	 * @author Amr Draz
	 * @version 2011.05.02
	 * @category C4S14
	 * @param id: id of the entity that is to be shown
	 * @return none
	 */
	
	public static void show(long id){
		
		Eentity entity = Eentity.findById(id);
		/// Draz's check for org privacy
		String username = Security.connected();
		User user= User.findByUsername(username);
		if(entity.getOrganization().isPrivate() && !(entity.getOrganization().isMember(user)))
			render("errors/503.html");
		//end  draz part
		System.out.println(entity.id+" show");
		render(entity, user);
	}

	public Topic createTopic(User initiator, String title, String content,
			String type, Eentity entity) {

		if (initiator.getRoleList().contains("Organizer")) {
			for (int i = 0; i < entity.getTopics().size(); i++) {
				if (entity.getTopics().get(i).getTitle() == title) {

					return null;
				}
			}
			Topic t = new Topic(initiator, title, content, type);
			t.setEntity(entity);
			t.save();
			return t;
		} else

			return null;
	}


	/**
	 * Allows the organization lead to create entities in his organization
	 * 
	 * @author Ebtehal Abed
	 * @version 2011.04.25
	 * @category C2S1
	 * @params String entityName, String entityDescription,
	 *         Long organizationID: the Id of the organization inwhich an entity will be created in,
	 * @return nothing (void)
	 */

	public static void createEntity(String entityName,
			String entityDescription, long organizationID) {

		String username = Security.connected();
		Organization organization = Organization.findById(organizationID);
		User user 		   = User.findByUsername(username);
		String result = "";
		
		if (user.getUserType(organization).equalsIgnoreCase("Lead")) {
			for (int i = 0; i < organization.getAllEntities().size(); i++) {
				if (organization.getAllEntities().get(i).getName()
						.equalsIgnoreCase(entityName))
					result = "Entity already exist";
			  redirect("Entities/createEntityHelper", result);
			} 
///@{Entities.createEntitiyhelper(o.id)}
			Eentity entity = new Eentity(entityName, entityDescription);
			entity.save();
			organization.getAllEntities().add(entity);
			entity.setOrganization(organization);
			entity.setOwner(user);
			user.getEntityList().add(entity);
	    	organization.save();
			entity.save();
			result = "Entity created successfully";
			  redirect("Entities/createEntity", result);
			
		}

		else{
			result = "You are not authorized to use this option";
			  redirect("Entities/createEntityHelper", result);
	}
	}
	
	
	
	public static void createEntityHelper(long organizationID){
		Organization organization = Organization.findById(organizationID);
		render(organization, organizationID);
	}
	
	public static void viewEntity(long id){
		Eentity e = Eentity.findById(id);
		render(e, id);
	}
	
	
	/** 
	 * A method to set a user as an organizer in an entity and notify
	 * the user with the method sendNotification which also sends an email
	 * to the user notifying him/her of his/her new role.
	 * 
	 * @author	Rania Nader Saeed
	 * @version 2011.04.06
	 * @category C1S12
	 * @param	organizerID : id of the organizer to be added in an entity
	 * @param	entityID	: id of the entity in which an organizer is to be added
	 * @return	void
	 * @throws	EmailException 
	 */
	public static void setOrganizerinEntity(long organizerID, long entityID) throws EmailException{
		User organizer = User.findById(organizerID);
		Eentity entity = Eentity.findById(entityID);

		organizer.getRoleList().add(new Role(entity,"Organizer"));
		entity.getOrganizers().add(organizer);
		organizer.save();
		entity.save();
		
		String content = "Congratulations " + organizer.getfName()
		+ "\n You are now an organizer in the entity "
		+ entity.getName();
		organizer.sendNotification(organizer, "New Role in an Entity!", content);
		
		List users	= User.findAll();
		List ent	= Eentity.findAll();
		LinkedList<Eentity> myent = new LinkedList<Eentity>();
		for(int i = 0; i< ent.size(); i++){
			if (((Eentity) ent).getOrganizers().get(i) == organizer){
				myent.add(((Eentity) ent));
				myent.addAll(((Eentity) ent).getRelatedEntities());
			}
		}
		
		render(users, myent);
	}
	
	
	
	/**
	 * View to show all entities on the system
	 * 
	 * @author Hossam Saraya
	 * @version May 2, 2011
	 * @category C1S10
	 */
	public static void viewEntities(){
		List entities = Eentity.findAll();
		render(entities);
	}
	
	

	public static List getAllEntityTopics ( int entityID)
	{
			Eentity x = Eentity.findById(entityID);
			return(x.getTopics());
		
		
	}

	public static List getAllEntityTags ( int entityID)
	{
			Eentity x = Eentity.findById(entityID);
			return(x.getTags());
		
		
	}


}